import getModules from "./modules";
const PREFIX = `openharmony-node-polyfill.`;
const PREFIX_LENGTH = PREFIX.length;

export default function openharmonyPolyfill() {
  const mods = getModules();

  return {
    name: "rollup-plugin-openharmony-polyfill",
    resolveId(importee:string) {
      if (mods.has(importee)) {
        return { id: PREFIX + importee, moduleSideEffects: false };
      }
    },
    load(id: string) {
      if (id.startsWith(PREFIX)) {
        const importee = id.substr(PREFIX_LENGTH);
        return mods.get(importee);
      }
    },
  };
}
